package com.ihrm.system.controller;


import com.ihrm.common.entity.Constants;
import com.ihrm.common.entity.PageResult;
import com.ihrm.common.entity.Result;
import com.ihrm.common.exception.CommonException;
import com.ihrm.domain.system.Permission;
import com.ihrm.system.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/sys")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    /**
     * 保存
     */
    @PostMapping("/permission")
    public Result save(@RequestBody Permission permission) throws Exception {
        Integer num = permissionService.insert(permission);
        if(num > 0) {
            return Result.SUCCESS();
        }else {
            return Result.FAIL();
        }
    }

    /**
     * 修改
     */
    @PutMapping("/permission/{id}")
    public Result updatePermission(@PathVariable String id, @RequestBody Permission permission) {
        permission.setId(id);
        Integer num = permissionService.update(permission);
        if(num > 0) {
            return Result.SUCCESS();
        }else {
            return Result.FAIL();
        }
    }

    /**
     * 查询列表
     */
    @GetMapping("/permission")
    public Result findAll(Permission permission) {
        Set<Permission> list = permissionService.findAll(permission);
        return Result.SUCCESS(list);
    }

    /**
     * 根据ID查询
     */
    @GetMapping(value = "/permission/{id}")
    public Result findById(@PathVariable String id){
        Permission permission = permissionService.findById(id);
        if(permission == null) {
            throw new CommonException(Constants.FAIL_CODE, "没有该权限");
        }
        return Result.SUCCESS(permission);
    }

    /**
     * 根据id删除
     */
    @DeleteMapping(value = "/permission/{id}")
    public Result deleteById(@PathVariable String id){
        Integer num = permissionService.del(id);
        if(num > 0) {
            return Result.SUCCESS();
        }else {
            return Result.FAIL();
        }

    }
}

